#include <TFile.h>
#include <iostream>
#include <string>
#include <TTree.h>
#include <TH1D.h>
#include <TF1.h>
#include <TCanvas.h>
using namespace std;
string filePath = "../AnaData/";
void GetDiff(string fileName = "ToF_Ana_20251020_4")
{
    TCanvas *c1 = new TCanvas();
    // c1->Divide(2, 1);
    // TFile *file = new TFile("../RootData/ToF_Sort_Coin_ToFdata_0916.root", "read");
    TFile *file = new TFile((filePath + fileName + ".root").c_str(), "read");
    TTree *treeAna_ToF = new TTree();
    file->GetObject("treeAna_ToF", treeAna_ToF);
    double timeCh[0][18];
    treeAna_ToF->SetBranchAddress("timeCh", timeCh);
    // TF1* f1 = new TF1("f1", "gaus", -2, 0);
    TH1D *hTOF = new TH1D("hTOF", "hTOF", 10000, 0, 100000);
    TH1D *hDiff = new TH1D("hDiff", "hDiff", 100000, 19999, 20001);
    for (int i = 1; i < treeAna_ToF->GetEntries(); i++)
    {
        treeAna_ToF->GetEntry(i);
        double timeCh1 = timeCh[0][1];
        treeAna_ToF->GetEntry(i - 1);
        double timeCh2 = timeCh[0][1];
        double diff = timeCh1 - timeCh2;
        hDiff->Fill(diff);
    }
    //     treeAna_ToF->Draw("timeCh[0][1]>>hTOF(1000,0, 100000)", "", "goff");

    // for (int i = 1; i < hTOF->GetNbinsX(); i++)
    // {
    //     double diff = hTOF->GetBinContent(i + 1) - hTOF->GetBinContent(i);
    //     double bin_center = (hTOF->GetBinCenter(i) + hTOF->GetBinCenter(i + 1)) / 2.0;
    //     hDiff->Fill(bin_center, diff);
    // }
    // c1->cd(1);
    // hTOF->Draw();
    // c1->cd(2);
    hDiff->Draw();
    //  treeAna_ToF->Draw("tmDif_Tstart[0][1]>>hToF", "", "colz");
    //  treeAna_ToF->Draw("tmDif[1][3]>>hToF", "", "colz");

    c1->SetTitle(fileName.c_str());
    c1->Draw();
    c1->WaitPrimitive();
}
int main(int argc, char const *argv[])
{
    GetDiff();
    return 0;
}
